package com.example.todolist.controller;

import com.example.todolist.entity.Habit;
import com.example.todolist.mapper.HabitMapper;
import com.example.todolist.service.HabitService;
import com.example.todolist.service.TaskEachDayService;
import com.example.todolist.tools.ResponseBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/habit")
public class HabitController {
    @Autowired
    private HabitService habitService;
    @Autowired
    private HabitMapper habitMapper;
    @Autowired
    private TaskEachDayService taskEachDayService;

    @PostMapping("/insertHabit")
    public ResponseBody<Integer> insertHabit(@RequestParam Integer userId, @RequestBody Habit habit)
    {
        return habitService.insertHabit(habit);
    }
    @PostMapping("/checkForHabit")
    public ResponseBody<Integer> checkForHabit(@RequestParam Integer userId,@RequestParam Integer habitId)
    {
        if(habitService.checkForHabit(userId,habitId) == 0)
        {
            return new ResponseBody<>(401,"请不要重复打卡",null);
        }
        taskEachDayService.updateTaskEachDay(userId);
        habitService.checkForHabit(userId,habitId);
        return new ResponseBody<>(200,"success",1);
    }

    @GetMapping("/getHabit")
    public ResponseBody<Habit> getHabit(@RequestParam Integer userId,@RequestParam Integer habitId)
    {
        return new ResponseBody<>(200,"success",habitMapper.getHabit(userId,habitId));
    }
    @GetMapping("/getHabitId")
    public ResponseBody<Integer> getHabitId(@RequestParam Integer userId,@RequestParam String habitName)
    {
        return new ResponseBody<>(200,"success",habitMapper.getHabitId(userId,habitName));
    }
    @DeleteMapping("/deleteHabit")
    public ResponseBody<Integer> deleteHabit(@RequestParam Integer userId,@RequestParam Integer habitId)
    {
        habitMapper.deleteHabit(userId,habitId);
        return new ResponseBody<>(200,"success",1);
    }

    @GetMapping("/getAllHabit")
    public ResponseBody<List<Habit>> getAllHabit(@RequestParam Integer userId)
    {
        return new ResponseBody<>(200,"success",habitMapper.getAllHabit(userId));
    }

}
